<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div><el-tag type="info" effect="light"><b>{{ date }}</b></el-tag></div>
        <div>
          <el-select v-model="searchTerms.payerMode" placeholder="支付方式" clearable @change="getData">
            <el-option value="0" label="自付"/>
            <el-option value="1" label="代付"/>
          </el-select>
          <el-select v-model="searchTerms['sales_uin']" clearable filterable class="select" placeholder="业务员" @change="getData">
            <el-option v-for="(item, index) in salerList" :key="index" :label="`业务员：${item.sales_name}`" :value="item.sales_uin"/>
          </el-select>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData">
            <el-select slot="prepend" v-model="searchTerms.searchSelected" class="search-select">
              <el-option :value="0" label="订单号"/>
              <el-option :value="1" label="客户账号"/>
              <el-option :value="2" label="客户备注"/>
            </el-select>
          </el-input>
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row
                @sort-change="handleSortChange">
        <el-table-column prop="creatTime" label="创建时间" min-width="160" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.creat_time?scope.row.creat_time:'--' }}
          </template>
        </el-table-column>
        <el-table-column prop="deal_name" label="订单号" min-width="140" header-align="center" align="center"/>
        <el-table-column label="状态" min-width="120" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.deal_status }}
          </template>
        </el-table-column>
        <el-table-column label="客户账号/备注" min-width="180" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.owner_uin?scope.row.owner_uin:'--' }}
            <div class="remarks">{{ scope.row.client_remark }}</div>
          </template>
        </el-table-column>
        <el-table-column label="客户类型" min-width="120" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.client_type === 'new' ? '新拓': scope.row.client_type === 'old' ? '存量' : '指派' }}
          </template>
        </el-table-column>
        <el-table-column prop="goods_name" label="云产品" min-width="120" header-align="center" align="center"/>
        <el-table-column prop="balance" label="费用（元）" min-width="140" header-align="center" align="center">
          <template slot-scope="scope">
            <span class="real-price">{{ scope.row['goods_price'] }}元</span>
          </template>
        </el-table-column>
        <el-table-column label="支付方式" min-width="100" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.payer_mode==='0'?'自付':'代付' }}
          </template>
        </el-table-column>
        <el-table-column label="业务员" min-width="100" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row.sales_name || '--' }}
          </template>
        </el-table-column>
        <el-table-column label="操作" min-width="120" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button type="text" size="mini" @click="toDetails(scope.row)">订单详情</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :total="total"
        :layout="($store.getters.id===1||queryMatch(269))&&!searchTerms.sales_uin?'total, prev, pager, next, jumper':'prev, next'"
        class="pagination"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
  import {handleSortChange, handleSizeChange, handleCurrentChange, parseTime} from '@/utils/table'

  export default {
    data() {
      return {
        id: '',
        date: '',
        searchTerms: {
          searchValue: '',
          searchSelected: 0,
          payerMode: '',
          sales_uin: ''
        },
        // 业务员列表
        salerList: [],
        // 表格相关数据
        tableMaxHeight: document.documentElement.clientHeight - 300,
        tableLoading: false,
        currentPage: 1,
        pageSize: 30,
        total: 0,
        tableData: [],
        multipleSelection: []
      }
    },
    created() {
      this.id = this.$route.query.id;
      this.getSalerList();
    },
    methods: {
      handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
      getData() {
        this.tableLoading = true
        const data = {
          id: this.id,
          page_no: this.currentPage,
          page_size: this.pageSize,
          payerMode: this.searchTerms.payerMode,
          sales_uin: this.searchTerms.sales_uin
        }
        if (this.searchTerms.searchSelected === 0) {
          data.deal_names = this.searchTerms.searchValue
        } else if (this.searchTerms.searchSelected === 1) {
          data.owner_uin = this.searchTerms.searchValue
        } else if (this.searchTerms.searchSelected === 2) {
          data.client_remark = this.searchTerms.searchValue
        } else if (this.searchTerms.searchSelected === 3) {
          data.sales_name = this.searchTerms.searchValue
        }
        const url = 'tencent/getAnalysisData'

        this.$store.dispatch('GetConnect', {url, data}).then(res => {
          this.tableData = res.data.rows
          this.total = parseInt(res.data.total)
          this.date = res.data.date
          this.tableLoading = false
        }).catch(e => {
          this.tableLoading = false
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      },
      // 加载业务员列表
      getSalerList() {
        const url = 'tencent/getSaleByUser'
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.salerList = res.data;
          if (this.salerList.length > 0) {
            this.getData({});
          } else if (this.$store.getters.id === 1 || this.queryMatch(268)) {
            this.getData({});
          }
        }).catch(e => {
          this.tableLoading = false
          this.$message.error(e.msg + ',请刷新或联系管理员')
        })
      },
      toDetails(row) {
        this.$router.push({
          path: '../details',
          query: {
            deal_names: row.deal_name,
            showType: 0
          }
        })
      }
    }
  }
</script>

<style lang="scss" type="text/scss" scoped>
  @import '../../../../styles/index.scss';

  .box-1 {
    .header {
      @include flex-row(center, space-between);
      padding-bottom: 15px;
      flex-wrap: wrap;

      > div {
        flex-wrap: wrap;
        >.el-select {
          margin-left: 15px;
          width: 160px;
        }
      }

      .search-input {
        margin-left: 15px;
        width: 280px;
        .search-select{
          width: 100px;
        }
      }

      .do-btn {
        @extend .bg-green;
        margin-left: 2px;
      }
    }
  }
</style>
